<!DOCTYPE html>
<!--
  ~ Copyright (c) 2017.
  ~ @author Hulunliang
  ~ @since 2017/11/4
  ~
  -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>合作方人员管理</title>
</head>
<body>
<div th:fragment="main" id="addCooperatorStaffer">
    <link rel="stylesheet" th:href="@{/css/cooperatorGroup/addCooperatorStaffer.css}">
    <div class="content-title">
        <p><span>合作方人员管理</span>&nbsp;>&nbsp;<span>新建合作方人员</span></p>
    </div>
    <div class="content-bar">
        <form class="form-horizontal clearfix col-xs-offset-1">
            <div class="form-group">
                <label class="form-label" for="companyName">单位名称&nbsp;:&nbsp;</label>
                <input type="text" id="companyName" class="form-input" placeholder>
                <label class="form-label" for="name">姓名&nbsp;:&nbsp;</label>
                <input type="text" id="name" name="name" class="form-input" placeholder>
            </div>

            <div class="form-group">
                <label class="form-label" for="projectName">项目名称&nbsp;:&nbsp;</label>
                <input type="text" id="projectName" name="projectName" class="form-input" placeholder>
                <label class="form-label" for="mobile">手机号&nbsp;:&nbsp;</label>
                <input type="text" id="mobile" name="mobile" class="form-input" placeholder>
            </div>

            <div class="form-group">
                <label class="form-label" for="email">邮箱&nbsp;:&nbsp;</label>
                <input type="text" id="email" name="email" class="form-input" placeholder>
                <label class="form-label" for="workAs">从事工作&nbsp;:&nbsp;</label>
                <input type="text" id="workAs" name="workAs" class="form-input" placeholder>
            </div>

            <div class="form-group">
                <label class="form-label" for="duration">服务时长&nbsp;:&nbsp;</label>
                <input type="text" id="duration" name="duration" class="form-input" placeholder>
            </div>

            <div class="form-group">
                <label class="form-label" for="note">备注信息&nbsp;:&nbsp;</label>
                <textarea class="form-input" id="note" name="note" cols="" rows="4"></textarea>
            </div>


            <div class="form-group">
                <!-- <input type="button" value="返回" class="btn btn-default pull-right cancel" id="cancel"> -->
                <!-- <input type="button" value="保存" class="btn btn-primary pull-right save" id="save"> -->
                <div class="footer">
                    <a id="save" class="btn btn-cyan btn-bg" role="button">保存</a>
                    <a id="cancel" class="btn btn-cyan btn-bg" href="#qualitycontrol-cooperatorGroup-cooperatorStaffer"
                       role="button">返回</a>
                </div>
            </div>

        </form>

        <script th:src="@{/assets/jquery/plugins/jquery.ui.widget.js}"></script>
        <script th:src="@{/assets/jquery/plugins/jquery.iframe-transport.js}"></script>

        <script>
            // 新建
            $('#save').on('click', function () {
                if ($("#companyName").val().trim().length == 0) {
                    toastr.error("单位名称不得为空！");
                    return;
                }
                if ($("#companyName").val().trim().length > 25) {
                    toastr.error("单位名称不得超过25个字符！");
                    return;
                }
                if ($("#name").val().trim().length == 0) {
                    toastr.error("姓名不得为空！");
                    return;
                }
                if ($("#name").val().trim().length > 25) {
                    toastr.error("姓名不得超过25个字符！");
                    return;
                }
//                if ($("#projectName").val().length == 0) {
//                    toastr.error("项目名称不得为空！");
//                    return;
//                }
                if ($("#projectName").val().length > 25) {
                    toastr.error("项目名称不得超过25个字符！");
                    return;
                }


                var mobile = $("#mobile").val();
                if (mobile.length != 0) {
                if(!(/^1[3|4|5|7|8][0-9]\d{8}$/.test(mobile))) {
                    toastr.error("请输入正确的手机号！");
                    return;
                }
                }


                var email = $("#email").val();
                if (email.length != 0) {
                    var reg = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;
                    if (!reg.test(email)) {
                        toastr.error("请输入正确的邮箱！");
                        return;
                    }

                if (email.length > 50) {
                    toastr.error("邮箱不得超过50个字符！");
                    return;
                }
                }
//                if ($("#workAs").val().length == 0) {
//                    toastr.error("从事工作不得为空！");
//                    return;
//                }
                if ($("#workAs").val().length > 25) {
                    toastr.error("从事工作不得超过25个字符！");
                    return;
                }
//                if ($("#duration").val().length == 0) {
//                    toastr.error("服务时长不得为空！");
//                    return;
//                }
                if ($("#duration").val().length > 25) {
                    toastr.error("服务时长不得超过25个字符！");
                    return;
                }
//                if ($("#note").val().length == 0) {
//                    toastr.error("备注不得为空！");
//                    return;
//                }
                if ($("#note").val().length> 250) {
                    toastr.error("备注不得超过250个字符！");
                    return;
                }
                var formData = {
                    "name": $("#name").val(),
                    "companyName": $("#companyName").val(),
                    "projectName": $("#projectName").val(),
                    "mobile": $("#mobile").val(),
                    "email": $("#email").val(),
                    "workAs": $("#workAs").val(),
                    "duration": $("#duration").val(),
                    "note": $("#note").val()
                };

                $.ajax({
                    url: contextPath + '/CooperatorStaffer/createCooperatorStaffer',
                    type: 'POST',
                    data: JSON.stringify(formData),
                    cache: false,
                    contentType: "application/json",
                    success: function (data) {
                        if(data.code == 1) {
                            toastr.success("新建合作方人员，保存成功!");
                            location.hash = "#qualitycontrol-cooperatorGroup-cooperatorStaffer";
                        }else{
                            toastr.error(data.message,'创建失败！');
                        }
                        console.log(data);
                    },
                    error: function (data) {
                        toastr.error("操作失败，请稍后再试!");
                    }
                });
            });


            // 取消
            $('#cancel').on('click', function () {
                $('#add_form')[0].reset();
            });
        </script>
    </div>
</body>
</html>

